#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>


// my functions
#include <print_error.h>
#include <program_options_helper.h>

using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	char input_format;
	char output_format;

//	po::options_description mandatory_args("Mandatory Arguments");
//	mandatory_args.add_options()
//	("help,h",						"Display this message.");
	args.optional_args.add_options()
	("input_format,i", 		po::value<char>(&input_format)
								->default_value('p')
								->set_name("FORMAT"),
									"Input FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("output_format,o", 	po::value<char>(&output_format)->default_value('p')->set_name("FORMAT"),
									"Output FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("zero_weight_nulls,z",			"Add a weight term of zero for "
									"matrix elements which are null or missing.\x1"
									"For programmes, e.g. fitch, kitch, taking weighted "
									"matrices.\x1"
									"Only applies to 's' and 'l' output formats." )
	("names_filter,n", 		po::value<string>(&args.names)
								->set_name("FILE"),
									"Only print matrix rows and columns with names "
									"matching those specified in FILE. ")
	("regex_filter,r", 		po::value<string>(&args.regex)->set_name("REGEX"),
									"Only print matrix rows and columns with names "
									"matching the regular expression REGEX.")
	("alphabetic_order,a",			"Print with names sorted alphabetically")
	("header,H",					"Add a header line (matrix name prefixed with '>') "
									"to the matrix and tree output.")
	("short_names,S", 				"Use 10 letter dummy names. Useful for phylip.")
	("names_map,N", 		po::value<string>(&args.name_mapping_file)
								->set_name("FILE")
								->default_value("short_to_long.names"),
									"The mapping of long names to short names will be "
									"in tab delimited columns. Only used if the previous "
									"option is specified.\n");


	args.add_std_options();
	po::options_description all_args("");
	all_args.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.1\n"
			"\tHistory: \n\n"
			"\tv 1.0\1\n"
			"\tv 1.1\1"
			"03-05-2005\1"
			"Changed matrix binary format to allow symmetrical matrices to have a specified diagonal.\1"
  			"No backwards compatibility :(\n"
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
				"Takes a distance matrix from STDIN "
				"and outputs it in the specified format.\n"
				"The data can be filtered to only include names matching either "
				"a regular expression or those supplied in a file.\n\n ";
		print_usage(cerr, string(*argv), "[OPTIONS] < MATRIX_FILE",
							all_args, exe_description, 80);
		return false;
	}

	args.header		= vm.count("header");
	args.zero		= vm.count("zero_weight_nulls");
	args.alpha		= vm.count("alphabetic_order");
	args.short_names= vm.count("short_names");


	select_matrix_file_format(input_format, args.input_format, "input format");
	select_matrix_file_format(output_format, args.output_format, "output format");


	args.open_err_log();
	return true;
}





